#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>
#include "form.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    database =new mysql;
    query=QSqlQuery(database->database);
    num_select=0;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    QPixmap pixmap("D:/qtname/QT/QTchar/pic/QQ.png");
    painter.drawPixmap(this->rect(),pixmap);

}

void MainWindow::sql_test(QString str)
{
    if(!query.exec(str))
    {
        qDebug()<<query.lastError();
    }
    else
    {
        qDebug()<<"ok";
        if(num_select==1)
        {
            if(query.next())
            {Form * f=new Form;
                num_select=0;
                this->hide();
                f->show();
            }
            else
            {
                QMessageBox::about(this,"登陆失败","账号或密码错误");
                ui->lineEdit->clear();
                ui->lineEdit_2->clear();
            }
        }
    }
}

void MainWindow::on_pushButton_clicked()//登陆
{
    QString name=ui->lineEdit->text();
    QString password=ui->lineEdit_2->text();
    num_select=1;
    QString str= QString("SELECT * FROM playlist WHERE name=%0 and password=%1;").arg(name).arg(password);
    sql_test(str);

}

void MainWindow::on_pushButton_2_clicked()//注册
{
    QString name=ui->lineEdit->text();
    QString password=ui->lineEdit_2->text();
    num_select=2;
    QString str= QString( "INSERT INTO playlist VALUES ('%0','%1');").arg(name).arg(password);
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    QMessageBox::about(this,"注册","账号注册成功");
    sql_test(str);
}

